package cn.xing.managementSystem.controller;

import cn.xing.managementSystem.bean.ResponseContent;
import cn.xing.managementSystem.bean.pageable.PageableRequest;
import cn.xing.managementSystem.bean.pageable.PageableResponse;
import cn.xing.managementSystem.domain.Course;
import cn.xing.managementSystem.domain.Information;
import cn.xing.managementSystem.domain.Student;
import cn.xing.managementSystem.enums.InformationType;
import cn.xing.managementSystem.service.StudentService;
import cn.xing.managementSystem.utils.Preconditions;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.List;

/**
 * @author cuixing
 * @package cn.xing.managementSystem.controller
 * @class StudentController
 * @email xing.cui@magicwindow.cn
 * @date 2018/4/25 下午1:31
 * @description
 */

@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/studentList")
    public String studentList(){
        return "content/studentList";
    }

    @Value("${image.url}")
    private String imageUrl;

    @RequestMapping("/studentPageItems")
    @ResponseBody
    public ModelAndView studentPageItems(PageableRequest pageableRequest){
        ModelAndView mv = new ModelAndView("content/studentItems");
        PageableResponse<Student> pageableResponse = studentService.listPageable(pageableRequest);
        mv.addObject("pageableResponse", pageableResponse);
        return mv;
    }

    @RequestMapping("/editStudent")
    public ModelAndView editStudent(Long studentId){
        Student student = null;
        ModelAndView mv = new ModelAndView();
        if(Preconditions.isNotBlank(studentId)){
            student = studentService.findById(studentId);
        }else{
            student = new Student();
        }
        mv.addObject("student", student);
        mv.setViewName("content/editStudent");
        return mv;
    }


    @RequestMapping("/saveStudent")
    @ResponseBody
    public ResponseContent saveStudent(Long studentId, String introduction,
                                      Long studentImageId, @RequestParam("studentImageFile") MultipartFile studentImageFile) throws IOException {
        Student student;
        if(Preconditions.isNotBlank(studentId)){
            student = studentService.findById(studentId);
        }else{
            student = new Student();
        }
        student.setIntroduction(introduction);
        if(studentService.saveStudentAndImageStore(student, studentImageId, studentImageFile)){
            return ResponseContent.buildSuccess("保存成功");
        }
        return ResponseContent.buildFail("保存失败");
    }


    @RequestMapping("/deleteStudentById")
    @ResponseBody
    public ResponseContent deleteStudentById(Long studentId) {
        Student student = studentService.findById(studentId);
        student.setDeleted(true);
        student = studentService.saveStudent(student);
        if(Preconditions.isNotBlank(student)){
            return ResponseContent.buildSuccess("删除成功");
        }
        return ResponseContent.buildFail("删除失败");
    }


    @RequestMapping("/v1/getStudentList")
    @ResponseBody
    public ResponseContent<JSONArray> getStudentList() {
        List<Student> students = studentService.findAllStudent();
        JSONArray jsonArray = new JSONArray();
        if(Preconditions.isNotBlank(students)){
            for(Student student : students){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("introduction", student.getIntroduction());
                jsonObject.put("image_url", String.format(imageUrl, student.getImageStore().getId()));
                jsonArray.add(jsonObject);
            }
        }
        return ResponseContent.buildSuccess(null, jsonArray);
    }
}